function can_municipal_agency(argument) {
	return this.getPerm().can_municipal_agency;
};
var skill = {
	setMAConfig: function(new_config) {
		var config = this.getMAConfig();
		var keys = [
			//市代订单收益
			"rake",
			//介绍市代收益
			"recomment_MA_earn_rake",
			//介绍门店收益
			"recomment_store_earn_rake",
			//异店补贴
			"dif_store_subsidy"
		];
		keys.forEach((key) => {
			if (new_config.hasOwnProperty(key)) {
				// 白分之rake
				var v = parseFloat(new_config[key]);
				if (isFinite(v) && 0 <= v && v <= 100) {
					config[key] = parseFloat(v);
				} else {
					throwCusError(`config "${key}" rang error(0~100)`)
				}
			}
		});
		return config;
	},
	getMA: function(MA_id) {
		console.log(MA_id)
		if (this._getMAIDList().indexOf(MA_id) === -1) {
			throwCusError("指定市级代理不属于商家")
		}
		var MA = BusMACon.get(MA_id);
		if (!MA) {
			throwCusError("找不到指定市级代理")
		}
		return MA;
	},
	getMADetail: function(MA_id) {
		return this.getMA(MA_id).getDetail();
	},
	//获取市代业绩
	getMAListWithAchDetailListByFilter: function(options) {
		options || (options = {});
		return this.getMAList().map(MA => {
			return {
				MA: MA,
				achs: MA.getAchDetailListByFilter(options.filter)
			}
		});
	},
	//新增市级代理
	addMA: function(MA_info) {
		MA_info.bus_id = this._id;
		var MA = new BusMACon(MA_info);
		var MA_id_list = this._getMAIDList();
		MA_id_list.unshift(MA._id);
		this.MAs = MA_id_list;
		this.save();
		return MA;
	},
	//修改市级代理信息
	updateMA: function(MA_id, MA_info) {
		var MA = this.getMA(MA_id);
		MA.setInfo(MA_info);
		return MA;
	},
	removeMA: function(MA_id) {
		var MA = this.getMA(MA_id);
		MA.destroy();
		var MA_id_list = this._getMAIDList();
		var _index = MA_id_list.indexOf(MA_id);
		MA_id_list.splice(_index, 1);
		this.MAs = MA_id_list;
		this.save();
	},
	/*
	 * 门店
	 */
	//不安全，增加与市代无关的直属门店
	_addShop: function(bus_shop_id) {
		var shops = this.getBusShopIDList();
		if (shops.indexOf(bus_shop_id) === -1) {
			shops.push(bus_shop_id);
			this.save();
		}
	},
	_removeShop: function(bus_shop_id) {
		var shops = this.getBusShopIDList();
		$Array.remove(shops, bus_shop_id)
		this.save();
	},
	addMAShop: function(shop_info) {
		shop_info.bus_id = this._id;
		return new BusShopCon(shop_info);
	},
	updateMAShop: function(shop_id, shop_info) {
		var shop = this.getMAShopById(shop_id);
		shop.setInfo(shop_info);
		return shop;
	},
	removeMAShop: function(shop_id) {
		var shop = this.getMAShopById(shop_id);
		shop.destroy();
	},
};
//权限限制注入
Object.keys(skill).forEach(function(method_name) {
	skill[method_name] = skill[method_name].perm(can_municipal_agency, "权限不足， 需要与管理员联系才能使用市代功能")
});
skill.canMA = can_municipal_agency;
skill = Object.assign(skill, {
	/*
	 * 不受权限限制的
	 */
	// 获取市级代理的统一配置
	getMAConfig: function() {
		return Object.assign({
			rake: 0,
			recomment_earn_rake: 0,
			recomment_MA_earn_rake: 0,
			recomment_store_earn_rake: 0,
			dif_store_subsidy: 0,
		}, this.MA_config || (this.MA_config = {}));
	},
	_getMAIDList: function() {
		if (!Array.isArray(this.MAs)) {
			this.MAs = []
			this.save();
		}
		return this.MAs;
	},
	getMAList: function() {
		var MA_id_list = this._getMAIDList()
		var res = MA_id_list.map((MA_id) => {
			try {
				return this.getMA(MA_id);
			} catch (e) {}
		}).filter(MA => MA);
		if (res.length !== MA_id_list.length) {
			this.MAs = res.map(MA => MA._id)
			this.save();
		}
		return res
	},
	//带有详细信息的
	getMADetailList: function() {
		return this._getMAIDList().map((MA_id) => {
			return this.getMADetail(MA_id);
		});
	},
	/*
	 * 门店
	 */
	// 直属商家的门店，与市代无关
	getBusShopIDList: function() {
		return this.shops || (this.shops = []);
	},
	getBusShopList: function() {
		return this.getBusShopIDList().map(shop_id => BusShopCon.get(shop_id));
	},
	getMAShopById: function(shop_id) {
		var shop = BusShopCon.get(shop_id);
		if (!shop) {
			throwCusError("找不到指定门店");
		}
		if (this._id !== shop.bus_id) {
			throwCusError("指定门店不属于商家");
		}
		return shop;
	},
	getMAShopList: function(MA_id) {
		var MA = this.getMA(MA_id);
		return MA.getShopList()
	},
	getAllShopList: function() {
		return this._getMAIDList().reduce((res, MA_id) =>
			res.concat(this.getMAShopList(MA_id)), this.getBusShopList());
	},
	getAllShopListByState: function(state) {
		console.log(state, typeof state);
		return this.getAllShopList().filter(shop => shop.state === state);
	}
});

module.exports = skill;

var db = require("../db/index");
var DomainCon = require("../domain/_con");
var GoodsCon = require("../goods/_con");
var UserCon = require("../../core/user/_con");
var BusCon = require("../../core/bus/_con");
var BusMACon = require("../bus.MA/_con");
var BusShopCon = require("../bus.shop/_con");
var AchCon = require("../ach/_con");
var ClassifyCon = require("../bus.classify/_con");
var TagCon = require("../bus.tag/_con");
var LeagueCon = require("../league/_con");
var BuffCon = require("../buff/_con");
var BusUserBindCon = require("../bus_user_bind/_con");
var BusGuideCon = require("../bus.guide/_con");
var CommentCon = require("../comment/_con");
var config = require("../_include/config");
var data_cut = require("../_include/data_cut");
var helper = require("../_include/helper");
var md5 = require("../_include/md5");
var $Object = require("../_include/$.Object");
var $Array = require("../_include/$.Array");
var tld = require('tldjs');
var fs = require("fs");
var Fiber = require('fibers');